<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">
		<view v-if="isLogin" class="body">			 
			<!-- 创建 -->
			<template v-if="type=='add'||type=='edit'">
				<view class="goods-wrap m-b2"> 			
					<u--form :model="params" ref="uForm" class="form-wrap">  
						<u-form-item label="项目名称" labelWidth="160" prop="num" borderBottom="true" class="u-form-item">
							<u-input type="text" v-model="params.name" placeholder="输入项目名称" disabledColor="#ffffff" border="none"/> 
						</u-form-item>	
						<view class="project-form-wrap m-t2 ">
							<!-- <view class="hd-title">上传文件</view> -->
							<view class="upload-area">
								<c-uploadFile-tool :files="files" @getFiles="getFiles"></c-uploadFile-tool> 							
							</view>
						</view>		 				
					</u--form>
				</view>	
				
			</template>
			
			<template v-if="type=='detail'">
				<view class="goods-wrap m-b2"> 			
					<u--form :model="params" ref="uForm" class="form-wrap">  
						<u-form-item label="项目名称" labelWidth="160" prop="num" borderBottom="true" class="u-form-item">
							<u-input type="text" v-model="params.name" disabled placeholder="输入项目名称" disabledColor="#ffffff" border="none"/> 
						</u-form-item>	
						<view class="project-form-wrap m-t2 ">
							<!-- <view class="hd-title">上传文件</view> -->
							<view class="upload-area">
								<c-uploadFile-tool type='1' :files="files" @getFiles="getFiles"></c-uploadFile-tool> 							
							</view>
						</view>		 				
					</u--form>
				</view>	
				
			</template>
			
			 
		</view> 
		 
		
		<template v-if="type=='edit'">
			<view v-if="!loadingState.default" class="footer">
				<view class="fix-wrap created-btn-wrap">
					<view class="btn submit-btn" @click="submit">保存编辑</view> 
				</view>
			</view>
		</template>
		
		<template v-if="type=='add'">
			<view class="footer">
				<view class="fix-wrap created-btn-wrap">
					<view class="btn submit-btn" @click="submit">提交</view> 
				</view>
			</view>
		</template>
		
	</view>
</template>

<script>
	import {
			checkPhone,dateFormat
		} from '@/common/util.js'
		
	const d = new Date()
	let currrentTimespace=d.getTime(); 
	let timespace_31=currrentTimespace+31*24*60*60*1000;	
	let format_date_31=dateFormat(timespace_31,'YYYY-MM-DD'); 
	export default {
		data() {
			return {
				isIphoneX:getApp().globalData.isIphoneX,
				isLogin: false,
				deviceslist:[[]], 
				files:[],
				id:'',
				type:'add',
				isShowTool:false,
				isShowDate:false,
				params:{ 
					cate:'',
					name:'',
					ids:[]
				},
				isStop:true,
				loadingState:{
					default:true
				},
				defaultDate:[format_date_31],
				minDate:format_date_31
			}
		},
		async onLoad(params) { 
			this.isLogin=await this.$loginState('check');
			this.id=params.id; 
			this.type=params.type;
			
			if(this.type=='edit'){
				uni.setNavigationBarTitle({
					title:'修改项目'
				})
			}else if(this.type=='detail'){
				uni.setNavigationBarTitle({
					title:'项目详情'
				})
			}else{
				uni.setNavigationBarTitle({
					title:'添加项目'
				})
			}
			
			if(this.isLogin){  
				this.deviceslistsel();
				 
				if(this.type=='edit'||this.type=='detail'){
					this.getDetail();
				}
				 
			}
			 
					 
		},
		methods: {
			
			getFiles(data){
				console.log(data)
				this.files=data;
			},
			
			async addGoods(){
				this.params.needNum+=1; 
				let data={
					id:'',
					device_id:"",
					device_name:'',
					num:"" 
				}; 
				this.$set(this.params.need,this.params.needNum,data);  
				
			},
			
			delGoods(idx){
				this.params.needNum-=1;				 
				this.params.need.splice(idx,1);
			},
			
			changepcategory(e){
				let idx=this.params.goodsIndex;
				this.params.need[idx].device_id=e.value[0].id;
				this.params.need[idx].device_name=e.value[0].name; 
				this.isShowTool=false; 
			},
			
			async deviceslistsel(){
				let res=await this.$api.user_api.deviceslistsel();  
				if(res.data.code==200){ 
					this.$set(this.deviceslist,0,res.data.data); 				 
				}
			},
			
			confirmDate(e){ 
				this.params.delivery_time=e[0];
				this.isShowDate=false;				
			},
			
			changeDevice(e){
				this.params.device_id=this.deviceslist[e.target.value].id;
				this.params.devices_name=this.deviceslist[e.target.value].name;
			}, 
			
			// 获取项目详情
			async getDetail(){
				let res=await this.$api.user_api.scireportinfo({
					id:this.id
				})  
				console.log(res)
				if(res.data.code==200){
					this.loadingState.default=false; 		 				
					this.params.name=res.data.data.name; 
					this.files=res.data.data.files;
				}
			},
			
			 
			
			// 切换期限类型
			changeDate(e){ 
				this.params.delivery_time=e.target.value; 
			},
			 
			
			// 创建项目
			async submit(){
				let _this=this; 
				let f_ids=[];
				let {
					cate, 
					name 
				} = this.params;  
				 
				
				if(this.isStop){
					this.isStop=false; 
					
					if(this.type=='add'){ 
						this.files.forEach(i=>{
							f_ids.push(i.id)
						})  
						let res = await this.$api.user_api.scireportadd({
							name,
							f_ids:f_ids
						});  
						
						if (res.data.code == 200) {
							this.$showToast({
								icon:'success',
								title:'添加成功',
								duration:1000,
							}).then(()=>{
								this.$store.commit('XQ_LIST_UPDATE',1);
								uni.navigateBack({
									delta:1
								})
							})
							this.isStop=true; 
						} else {
							this.$showToast({
								icon:'error',
								title:res.data.msg,
							}) 
							this.isStop=true; 
						}
					}
					
					// 编辑
					if(this.type=='edit'){ 
						console.log(this.files)
						this.files.forEach(i=>{
							f_ids.push(i.id)
						}) 
						let res = await this.$api.user_api.scireportedit({
							id:this.id, 
							name,
							f_ids:f_ids
						});  
						if (res.data.code == 200) {
							this.$showToast({
								icon:'success',
								title:'编辑成功',
								duration:1000,
							}).then(()=>{
								this.$store.commit('XQ_LIST_UPDATE',1);
								uni.navigateBack({
									delta:1
								})
							})
							this.isStop=true; 
						} else {
							this.$showToast({
								icon:'error',
								title:res.data.msg,
							}) 
							this.isStop=true; 
						}
					}
					
				}
				
			}
			
			
		}
	}
</script>

<style lang="less" scoped>
	.project-form-wrap{
		padding: 20rpx 0 0; 
		.form-wrap{
			 
		}
	}
	.hd-title{
		margin:0 20rpx 20rpx;
		padding-left:20rpx;
		border-left: 8rpx solid #0099ff;
		font-size: 30rpx;
		font-weight: bold;
	}
	// .hd-title{
	// 	display: flex;
	// 	justify-content: flex-end;
	// 	padding: 20rpx;
	// }
	
	.created-btn-wrap{
		display: flex;
		.btn{
			height: 80rpx;
			line-height: 80rpx;
			margin: 10rpx 10rpx 0;
			flex-grow: 1;
			border-radius: 8rpx;
			text-align: center;
			font-size: 30rpx;
			color:#fff;
		}
		.next-btn{
			background: #ff9600; 
		}
		.submit-btn{
			background: #0099ff; 
		}
	} 
	.step-bar{
		height: 90rpx;
		margin-bottom: 20rpx;
		.fix-wrap{
			display: flex;
			height: 90rpx;
			background: #fff;
		}
		.item{
			flex-grow: 1;
			width: 25%;
			text-align: center;
			color:#999;
			.text{
				font-size: 28rpx;				
			}
			.icon{
				width: 40rpx;
				height: 40rpx;
				margin:0 auto 0; 
			}
		}
		.active{
			.text,.icon{
				color:#09f;				
			} 
		}
	}
	.form-wrap{
		padding:0 20rpx; 
		.row-2{
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
</style>
